import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;import java.io.BufferedReader;
 
import java.io.InputStreamReader;
 
import java.sql.Connection;
 
import java.sql.DriverManager;
 
import java.sql.ResultSet;
 
import java.sql.Statement;
 
import java.util.Calendar;
 

 
public class MainClass {
 
        
 
        static Statement stmt;
 
        static String sql;
 
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        
 
        static int cnt;
 
        static ResultSet rs;
 
        
 
        public static void jdbc() throws Exception{
 
                String driver = "oracle.jdbc.driver.OracleDriver";
 
                String url = "jdbc:oracle:thin:@localhost:1521:XE";
 
                String id = "cristalriver";
 
                String password = "tt4ever";
 
                
 
                Class.forName(driver);
 
                Connection conn = DriverManager.getConnection(url, id, password);
 
                
 
                if(conn != null)
 
                        System.out.println("DB 접속 성공");
 
                else
 
                        System.out.println("DB 접속 실패");
 
                
 
                stmt = conn.createStatement();
 
        }
 
        
 
        public static String nowDate() {
 
                String result = "2014/03/01";
 
                
 
                Calendar date = Calendar.getInstance();
 
                String yy = String.valueOf(date.get(Calendar.YEAR));
 
                String mm = String.valueOf(date.get(Calendar.MONTH)+1);
 
                String dd = String.valueOf(date.get(Calendar.DAY_OF_MONTH));
 
                
 
                if(mm.length()==1){
 
                        mm = "0"+mm;
 
                }
 
                if(dd.length()==1){
 
                        dd = "0"+dd;
 
                }
 
                
 
                result =yy+"/"+mm+"/"+dd; 
 
                
 
                return result;
 
        }
 
        
 
        public static int maxNum() throws Exception{
 
                int result = 0;
 
                
 
                sql = "select num from board order by num desc";
 
                rs = stmt.executeQuery(sql);
 
                
 
                if(rs.next() != false)
 
                        result = rs.getInt(1);
 
                
 
                return result+1;
 
                
 
        }
 
        
 
        public static void countUp(String num) throws Exception{
 
                int result = 0;
 
                
 
                sql = String.format("update board set readcount = readcount+1 where num = %s",num);
 
                cnt = stmt.executeUpdate(sql);
 
                
 
                if(cnt != 0)
 
                        System.out.println("조회수 증가!");
 
                
 
        }
 
        
 
        public static void addData() throws Exception{
 
                System.out.println("제목을 입력하세요");
 
                String title = br.readLine();
 
                System.out.println("내용을 입력하세요");
 
                String content = br.readLine();
 
                
 
                sql = String.format("insert into board values (%s,'%s','%s','%s',0)",maxNum(),title,content,nowDate());
 
                System.out.println(sql);
 
                cnt = stmt.executeUpdate(sql);
 
                
 
                if(cnt !=0)
 
                        System.out.println("추가성공!");
 
                else
 
                        System.out.println("추가실패");
 
                
 
        }
 
        public static void listData() throws Exception{
 
                
 
                sql = "select * from board order by num";
 
                rs = stmt.executeQuery(sql);
 
                
 
                System.out.println("번호\t제목\t내용\t날짜\t조회수\t");
 
                while(rs.next() != false){
 
                        
 
                        System.out.print(rs.getInt(1)+"\t"); 
 
                        System.out.print(rs.getString(2)+"\t"); 
 
                        System.out.print(rs.getString(3)+"\t"); 
 
                        System.out.print(rs.getString(4)+"\t"); 
 
                        System.out.println(rs.getInt(5)); 
 
                }
 
                
 
        }
 
        public static void searchData() throws Exception{
 
                
 
                System.out.println("찾을 번호는?");
 
                String search = br.readLine();
 
                countUp(search);
 
                sql = String.format("select * from board where num = %s",search);
 
                rs = stmt.executeQuery(sql);
                
 
                System.out.println();
 
                
 
                while(rs.next() != false){
 
                        
 
                        System.out.print(rs.getInt(1)+"\t"); 
 
                        System.out.print(rs.getString(2)+"\t"); 
 
                        System.out.print(rs.getString(3)+"\t"); 
 
                        System.out.print(rs.getString(4)+"\t"); 
 
                        System.out.println(rs.getInt(5)); 
 
                }
 
                
 
        }
 
        public static void deleteData() throws Exception{
 

 
                System.out.println("삭제할 번호를 입력하세요");
 
                String num = br.readLine();
 
                
 
                sql = String.format("delete from board where num = %s",num);
 
                //System.out.println(sql);
 
                cnt = stmt.executeUpdate(sql);
 
                
 
                if(cnt !=0)
 
                        System.out.println("삭제성공!");
 
                else
 
                        System.out.println("삭제실패");
 
                
 
        }
 
        
 
        
 
        public static void main(String[] args) throws Exception {
 
                jdbc();
 
                
 
                while(true){
 
                        System.out.println("A.추가|L.목록|S.찾기|D.삭제|Q.종료");
 
                        String menu = br.readLine();
 
                        switch(menu){
 
                        case "A" : 
 
                        case "a" : 
 
                                addData();
 
                                break;  
      
                        case "L" : 
 
                        case "l" : 
 
                                listData();
 
                                break;  
 
                        case "S" : 
 
                        case "s" : 
 
                                searchData();
 
                                break;  
 
                        case "D" : 
 
                        case "d" : 
 
                                deleteData();
 
                                break;  
 
                        case "Q" : 
 
                        case "q" : 
 
                                System.exit(0);
 
                                break;
 
                        default : 
 
                                break;
 
                        }
 
                }
 
        }
 
} 


public class MainClass {
	
	static Statement stmt;
	static String sql;
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
	static int cnt;
	static ResultSet rs;
	
	public static void jdbc() throws Exception{
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:XE";
		String id = "cristalriver";
		String password = "tt4ever";
		
		Class.forName(driver);
		Connection conn = DriverManager.getConnection(url, id, password);
		
		if(conn != null)
			System.out.println("DBÁ¢¼Ó ¼º°ø");
		else
			System.out.println("DBÁ¢¼Ó ½ÇÆÐ");
		
		stmt = conn.createStatement();
	}
	
	public static String nowDate() {
		String result = "2014/03/01";
		
		Calendar date = Calendar.getInstance();
		String yy = String.valueOf(date.get(Calendar.YEAR));
		String mm = String.valueOf(date.get(Calendar.MONTH)+1);
		String dd = String.valueOf(date.get(Calendar.DAY_OF_MONTH));
		
		if(mm.length()==1){
			mm = "0"+mm;
		}
		if(dd.length()==1){
			dd = "0"+dd;
		}
		
		result =yy+"/"+mm+"/"+dd; 
		
		return result;
	}
	
	public static int maxNum() throws Exception{
		int result = 0;
		
		sql = "select num from board order by num desc";
		rs = stmt.executeQuery(sql);
		
		if(rs.next() != false)
			result = rs.getInt(1);
		
		return result+1;
		
	}
	
	public static void countUp(String num) throws Exception{
		int result = 0;
		
		sql = String.format("update board set readcount = readcount+1 where num = %s",num);
		cnt = stmt.executeUpdate(sql);
		
		if(cnt != 0)
			System.out.println("Á¶È¸¼ö1Áõ°¡");
		
	}
	
	public static void addData() throws Exception{
		System.out.println("Á¦¸ñÀ» ÀÔ·ÂÇÏ¼¼¿ä.");
		String title = br.readLine();
		System.out.println("³»¿ëÀ» ÀÔ·ÂÇÏ¼¼¿ä.");
		String content = br.readLine();
		
		sql = String.format("insert into board values (%s,'%s','%s','%s',0)",maxNum(),title,content,nowDate());
		System.out.println(sql);
		cnt = stmt.executeUpdate(sql);
		
		if(cnt !=0)
			System.out.println("ÀÔ·Â ¼º°ø!");
		else
			System.out.println("ÀÔ·Â ½ÇÆÐ");
		
	}
	public static void listData() throws Exception{
		
		sql = "select * from board order by num";
		rs = stmt.executeQuery(sql);
		
		System.out.println("¹øÈ£\tÁ¦¸ñ\t³»¿ë\t³¯Â¥\t\tÁ¶È¸¼ö");
		while(rs.next() != false){
			
			System.out.print(rs.getInt(1)+"\t"); 
			System.out.print(rs.getString(2)+"\t"); 
			System.out.print(rs.getString(3)+"\t"); 
			System.out.print(rs.getString(4)+"\t"); 
			System.out.println(rs.getInt(5)); 
		}
		
	}
	public static void searchData() throws Exception{
		
		System.out.println("Ã£À» ¹øÈ£´Â?");
		String search = br.readLine();
		countUp(search);
		
		sql = String.format("select * from board where num = %s",search);
		rs = stmt.executeQuery(sql);
		
		System.out.println();
		
		while(rs.next() != false){
			
			System.out.print(rs.getInt(1)+"\t"); 
			System.out.print(rs.getString(2)+"\t"); 
			System.out.print(rs.getString(3)+"\t"); 
			System.out.print(rs.getString(4)+"\t"); 
			System.out.println(rs.getInt(5)); 
		}
		
	}
	public static void deleteData() throws Exception{

		System.out.println("»èÁ¦ÇÒ ¹øÈ£¸¦ ÀÔ·ÂÇÏ¼¼¿ä.");
		String num = br.readLine();
		
		sql = String.format("delete from board where num = %s",num);
		//System.out.println(sql);
		cnt = stmt.executeUpdate(sql);
		
		if(cnt !=0)
			System.out.println("»èÁ¦ ¼º°ø!");
		else
			System.out.println("»èÁ¦ ½ÇÆÐ");
		
	}
	
	
	public static void main(String[] args) throws Exception {
		jdbc();
		
		while(true){
			System.out.println("A.Ãß°¡|L.¸ñ·Ï|S.Ã£±â|D.»èÁ¦|Q.Á¾·á");
			String menu = br.readLine();
			
			switch(menu){
			case "A" : 
			case "a" : 
				addData();
				break;	
			
			case "L" : 
			case "l" : 
				listData();
				break;	
			
			case "S" : 
			case "s" : 
				
				searchData();
				break;	
			
			case "D" : 
			case "d" : 
				deleteData();
				break;	
			
			case "Q" : 
			case "q" : 
				System.exit(0);
				break;
			
			default : 
				break;
			}
			
		}
		
	}
}
